# Compiler and build settings
GOCMD = go
BUILD_ENV := CGO_ENABLED=1
BUILD_TIME := $(shell date +%FT%T%z)
VERSION ?= v0.1.0  # Default version if not provided
LDFLAGS := -ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD_TIME}"

# Target binary paths
BIN_DIR := ../bin
TARGET := $(BIN_DIR)/growcode
TARGET_OSX := $(BIN_DIR)/growcode_for_osx
TARGET_LINUX := $(BIN_DIR)/growcode_for_linux
TARGET_WIN := $(BIN_DIR)/growcode_for_win.exe

# Ensure the bin directory exists
$(BIN_DIR):
	mkdir -p $(BIN_DIR)

# Build targets
.PHONY: all clean run build build-linux build-osx build-win

all: clean build build-linux build-osx build-win

clean:
	rm -rf $(TARGET)
	rm -rf $(TARGET_OSX)
	rm -rf $(TARGET_LINUX)
	rm -rf $(TARGET_WIN)

run:
	$(GOCMD) run main.go

build: $(BIN_DIR)
	${BUILD_ENV} GOOS=darwin $(GOCMD) build $(LDFLAGS) -o $(TARGET)

build-linux: $(BIN_DIR)
	${BUILD_ENV} GOARCH=amd64 GOOS=linux $(GOCMD) build $(LDFLAGS) -o $(TARGET_LINUX)

build-osx: $(BIN_DIR)
	${BUILD_ENV} GOOS=darwin $(GOCMD) build $(LDFLAGS) -o $(TARGET_OSX)

build-win: $(BIN_DIR)
	${BUILD_ENV} GOARCH=amd64 GOOS=windows $(GOCMD) build $(LDFLAGS) -o $(TARGET_WIN)
